<template>
  <div class="app-container">
    <div style="display:flex;margin-bottom:20px;">
      <div>
        <el-input v-model="param.search" size="small" :clearable="true" placeholder="请输入账号" />
      </div>
      <div style="margin-left:20px">
        <el-button type="primary" size="small" icon="el-icon-search" @click="searchList">搜索</el-button>
      </div>
    </div>
    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      border
      fit
      highlight-current-row
    >
      <el-table-column align="center" label="序号" width="100">
        <template slot-scope="scope">
          {{ scope.row.id }}
        </template>
      </el-table-column>
      <el-table-column label="用户账号" width="160" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.account }}</span>
        </template>
      </el-table-column>
      <el-table-column label="头像" width="110">
        <template slot-scope="scope">
          <img :src="scope.row.img" width="50px" height="50px">
        </template>
      </el-table-column>
      <el-table-column prop="password" label="用户密码" width="160" align="center" show-overflow-tooltip />
      <el-table-column class-name="status-col" label="状态" width="110" align="center" prop="status">
        <template slot-scope="scope">
          <el-tag :type="scope.row.status | statusFilter">{{ scope.row.status == 1 ?'正常':'禁用' }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="性别" align="center" width="110px">
        <template slot-scope="scope">
          <span v-if="scope.row.sex ==1">男</span>
          <span v-if="scope.row.sex ==2">女</span>
        </template>
      </el-table-column>
      <el-table-column label="用户生日" width="160" align="center">
        <template slot-scope="scope">
          <span v-if="scope.row.birthday ==0">无信息</span>
          <span v-else>{{ scope.row.birthday }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" prop="created_at" label="创建时间" width="200">
        <template slot-scope="scope">
          <i class="el-icon-time" />
          <span>{{ scope.row.created_at }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" prop="updated_at" label="更新时间" width="200">
        <template slot-scope="scope">
          <i class="el-icon-time" />
          <span>{{ scope.row.updated_at }}</span>
        </template>
      </el-table-column>
    </el-table>
    <div class="block">
      <span class="demonstration" />
      <el-pagination
        align="center"
        :current-page="param.page"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="param.limit"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        style="margin-top: 20px"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
  </div>
</template>
<script>
import { User } from '@/api/users'

export default {
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: 'success',
        draft: 'gray',
        deleted: 'danger'
      }
      return statusMap[status]
    }
  },
  data() {
    return {
      list: null,
      listLoading: true,
      total: '',
      dialogFormVisible: false,
      param: {
        page: 1,
        limit: 10,
        search: ''
      }
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    searchList() {
      this.fetchData()
    },
    fetchData() {
      this.listLoading = true
      User(this.param).then(response => {
        // console.log(response, 111)
        this.list = response.data.data
        this.total = response.data.total
        this.listLoading = false
      })
    },
    handleSizeChange(val) {
      this.param.limit = val
      console.log(`每页 ${val} 条`)
      this.fetchData()
    },
    handleCurrentChange(val) {
      this.param.page = val
      console.log(`当前页: ${val}`)
      this.fetchData()
    }
  }
}
</script>
